<template>
  <el-dialog
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    title="下发参数编辑"
    :append-to-body="true"
    :visible="stageVisible"
    width="40%"
    @close="close"
  >
    <el-popover
      popper-class="preview_popover"
      @show="showPreChart"
      placement="left"
      width="400"
    >
      <PreviewChart
        ref="previewChartRef"
        :chartData="form.formulaParameter"
      ></PreviewChart>
      <el-button
        icon="el-icon-view"
        style="margin-bottom: 10px;"
        slot="reference"
        size="small"
        >预览工艺曲线</el-button
      >
    </el-popover>
    <el-form
      class="iss_form"
      :model="form"
      ref="formName"
      :inline="false"
      size="normal"
    >
      <el-table :data="form.formulaParameter" border stripe size="small">
        <el-table-column prop="stageId" label="" align="center" width="60">
          <template slot-scope="scope"> {{ scope.$index + 1 }} </template>
        </el-table-column>
        <el-table-column prop="setTime" label="设定时间min" align="center">
          <template slot-scope="scope">
            <el-form-item
              :prop="`formulaParameter.${scope.$index}.setTime`"
              :rules="[
                {
                  required: true,
                  message: '设定时间不能为空',
                  trigger: 'blur'
                },
                {
                  validator(rule, value, callback) {
                    validatorForm(rule, value, callback, scope.row);
                  },
                  trigger: 'blur'
                }
              ]"
              label=""
            >
              <el-input
                v-model="scope.row.setTime"
                placeholder="请输入设定时间min"
                size="mini"
                clearable
              ></el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column prop="setTemp" label="设定温度℃" align="center">
          <template slot-scope="scope">
            <el-form-item
              :prop="`formulaParameter.${scope.$index}.setTemp`"
              :rules="[
                {
                  required: true,
                  message: '设定时间不能为空',
                  trigger: 'blur'
                },
                {
                  validator(rule, value, callback) {
                    validatorForm(rule, value, callback, scope.row);
                  },
                  trigger: 'blur'
                }
              ]"
              label=""
            >
              <el-input
                v-model="scope.row.setTemp"
                placeholder="请输入设定时间min"
                size="mini"
                clearable
              ></el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="volumeTemp" label="温度公差℃" align="center">
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.volumeTemp"
            placeholder="请输入温度公差℃"
            size="mini"
            clearable
          ></el-input>
        </template>
      </el-table-column> -->
        <el-table-column label="操作" align="center">
          <template slot="header">
            <div class="flex-x flex-center">
              <div style="margin-right: 10px">操作</div>
              <el-button type="primary" size="small" @click="addFormula"
                >添加</el-button
              >
            </div>
          </template>
          <template slot-scope="scope">
            <el-button type="danger" size="mini" @click="del(scope.$index)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-form>

    <span slot="footer">
      <el-button
        type="primary"
        :loading="stageBtnloading"
        @click="submit"
        size="small"
        >确认下发</el-button
      >
      <el-button @click="close" size="small"> 取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getTaskDownLoadInfos } from "@/api/deviceOperation";
import { generateId } from "@/utils";
import { valueIsNumber } from "@/utils/validatorForm";
import PreviewChart from "./PreviewChart.vue";
export default {
  components: {
    PreviewChart
  },
  props: {
    // 是否是下发按钮
    isIssued: {
      type: Boolean,
      default: false
    },
    stageVisible: {
      type: Boolean,
      default: false
    },
    stageBtnloading: {
      type: Boolean,
      default: false
    },
    formulaParameter: {
      type: Array,
      default: () => []
    },
    form: {
      type: Object,
      default: () => {
        return {
          formulaParameter: []
        };
      }
    }
  },
  computed: {
    validatorForm() {
      return function(rule, value, callback, row) {
        return valueIsNumber(rule, value, callback, row);
      };
    }
  },
  data() {
    return {
      showPopover: false
    };
  },
  watch: {},
  mounted() {
    // 获取任务下发弹框列表
    // this.getTaskDownLoadInfos();
  },
  methods: {
    // 监听预览弹框显示
    showPreChart() {
      this.$nextTick(() => {
        this.$refs.previewChartRef.init();
      });
    },
    // 删除阶段
    del(index) {
      this.form.formulaParameter.splice(index, 1);
    },
    submit() {
      this.$refs["formName"].validate(valid => {
        if (valid) {
          this.$emit("submit");
        }
      });
    },
    addFormula() {
      console.log("this.form :>> ", this.form);
      this.form.formulaParameter.push({
        setTime: "",
        setTemp: "",
        volumeTemp: "",
        id: generateId()
      });
    },
    close() {
      this.$emit("update:stageVisible", false);
    }
  }
};
</script>

<style>
.preview_popover {
  padding: 0 !important;
}
</style>
